package com.jiantong.user.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.jiantong.common.constants.JacksonType;
import com.jiantong.user.enums.OrganizationType;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

/**
 * Code generated by CodeGen
 * Desc: 组织机构信息Model类
 * Date: 2017-03-14
 */
@Data
public class Organization implements Serializable {

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = 959890684841559830L;

    private Long id;
    
    /**
     * 公司外部编码或者ID
     */
    private String outId;
    
    /**
     * 公司名称
     */
    private String name;
    
    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 状态，-1：不通过、0：待审核、1：审核通过
     */
    private Integer status;
    
    /**
     * 类型 1、养殖场 2、屠宰场 3、政府
     */
    private Integer type;
    
    /**
     * 额外信息
     */
    @JsonIgnore
    private String extraJson;

    private Map<String , Map<String ,String>> corporateCharterMap;

    /**
     * 公司执照
     */
    private String corporateCharter;
    
    /**
     * 创建时间
     */
    private Date createdAt;
    
    /**
     * 更新时间
     */
    private Date updatedAt;

    private Map<String, String> extra;

    public void setExtraJson(String extraJson) throws Exception{
        this.extraJson = extraJson;
        if(Strings.isNullOrEmpty(extraJson)){
            this.extra= Collections.emptyMap();
        } else{
            this.extra = objectMapper.readValue(extraJson, JacksonType.MAP_OF_STRING);
        }
    }

    public void setExtra(Map<String, String> extra) {
        this.extra = extra;
        if(extra ==null ||extra.isEmpty()){
            this.extraJson = null;
        }else{
            try {
                this.extraJson = objectMapper.writeValueAsString(extra);
            } catch (Exception e) {
                //ignore this fuck exception
            }
        }
    }

    public void setCorporateCharter(String corporateCharter){
        this.corporateCharter = corporateCharter;

        if(Strings.isNullOrEmpty(corporateCharter)){
            this.corporateCharterMap = Maps.newHashMap();
        }else {
            try {
                this.corporateCharterMap = objectMapper.readValue(corporateCharter, new TypeReference<Map<String , Map<String , String>>>() {});
            } catch (IOException e) {
                //ignore this fuck IOException
            }
        }
    }

    public void setCorporateCharterMap(Map<String , Map<String , String>> corporateCharterMap){
        this.corporateCharterMap = corporateCharterMap;

        if(corporateCharterMap == null || corporateCharterMap.isEmpty()){
            this.corporateCharter = null;
        }else {
            try {
                this.corporateCharter = objectMapper.writeValueAsString(corporateCharterMap);
            } catch (JsonProcessingException e) {
                //ignore this fuck JsonProcessingException
            }
        }
    }

    public Boolean isGovernment() {
        return Objects.equal(type, OrganizationType.GOVERNMENT.value());
    }

    public Boolean isButcher() {
        return Objects.equal(type, OrganizationType.BUTCHER.value());
    }

    public Boolean isBreeder() {
        return Objects.equal(type, OrganizationType.BREEDER.value());
    }
}
